﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;
using System.IO;
using Microsoft.Web.Administration;
using System.ServiceProcess;
using System.Diagnostics;
using System.Security.AccessControl;

namespace HostAssistantCore
{
 public    class AddSiteUtil
    {

        /// <summary>
        /// 添加系统(域)用户
        /// </summary>
        /// <param name="strUserName"></param>
        /// <param name="strPassword"></param>
        /// <param name="strDescription"></param>
        /// <param name="strGroup"></param>
        /// <returns></returns>
        public int AddUser_Q(string username, string password, string description, string group)
        {
            int errorCode = ErrorCode.UserAdd;
            string sitename = username;

            using (DirectoryEntry AD = new DirectoryEntry(@"WinNT://" + Environment.MachineName + ",computer"))
            {
                try
                {
                    DirectoryEntry User = null;
                    try
                    {
                        User = AD.Children.Add(username, "user");
                    }
                    catch
                    {

                    }

                    User.Invoke("SetPassword", new object[] { password });   // 密码
                    User.Properties["description"].Add(description);  //添加描述
                    User.Invoke("Put", "UserFlags", 66049); //密码永不过期
                    User.CommitChanges();

                    try
                    {
                        DirectoryEntry Group = AD.Children.Find(group, "group");
                        Group.Invoke("Add", new object[] { User.Path.ToString() });
                    }
                    catch
                    {

                    }
                }
                catch
                {
                    errorCode = ErrorCode.UserNotAdd;
                }
            }
            return errorCode;
        }

        /// <summary>
        /// 添加系统(域)用户
        /// </summary>
        /// <param name="strUserName"></param>
        /// <param name="strPassword"></param>
        /// <param name="strDescription"></param>
        /// <param name="strGroup"></param>
        /// <returns></returns>
        public int AddUser(string username, string password, string description, string group)
        {
            int errorCode = ErrorCode.UserAdd;
            string sitename = username;

            using (DirectoryEntry AD = new DirectoryEntry(@"WinNT://" + Environment.MachineName + ",computer"))
            {
                try
                {
                    DirectoryEntry User = null;
                    try
                    {
                        User = AD.Children.Find(username, "user");//找到要删除的用户
                        AD.Children.Remove(User);//删除用户
                        User = AD.Children.Add(username, "user");
                    }
                    catch (Exception ex)
                    {
                        NLogger.Error(ex.ToString());
                        User = AD.Children.Add(username, "user");
                    }

                    User.Invoke("SetPassword", new object[] { password });   // 密码
                    User.Properties["description"].Add(description);  //添加描述
                    User.Invoke("Put", "UserFlags", 66049); //密码永不过期
                    User.CommitChanges();

                    try
                    {
                        DirectoryEntry Group = AD.Children.Find(group, "group");
                        Group.Invoke("Add", new object[] { User.Path.ToString() });
                    }
                    catch (Exception ex)
                    {
                        NLogger.Error(ex.ToString());
                        DirectoryEntry Group = AD.Children.Add(group, "group");
                        Group.CommitChanges();
                        Group.Invoke("Add", new object[] { User.Path.ToString() });
                    }
                }
                catch (Exception ex)
                {
                    NLogger.Error(ex.ToString());
                    errorCode = ErrorCode.UserNotAdd;
                }
            }
            return errorCode;
        }
        /// <summary>
        /// 删除Windows用户
        /// </summary>
        /// <param name="username"></param>
        public void delUser(string username)
        {
            using (DirectoryEntry AD = new DirectoryEntry(@"WinNT://" + Environment.MachineName + ",computer"))
            {
                try
                {
                    DirectoryEntry User = null;
                    try
                    {
                        User = AD.Children.Find(username, "user");//找到要删除的用户
                        AD.Children.Remove(User);//删除用户
                    }
                    catch
                    {
                    }
                }
                catch
                {

                }
            }
        }
        /// <summary>
        /// 为站点指定应用程序池
        /// </summary>
        public void connectPool(string sitename, string poolname)
        {
            try
            {
                using (ServerManager iisManager = new ServerManager())
                {
                    Configuration cfg = iisManager.GetApplicationHostConfiguration();
                    ConfigurationSection sitesection = cfg.GetSection("system.applicationHost/sites");
                    var sitecollection = sitesection.GetCollection();
                    foreach (var siteinfo in sitecollection)
                    {
                        if (siteinfo.GetAttributeValue("name").ToString() == sitename)
                        {
                            siteinfo.GetCollection()[0].SetAttributeValue("applicationPool", poolname);
                        }
                    }
                    iisManager.CommitChanges();
                }
            }
            catch { }
        }
        /// <summary>
        /// 获取应用池名
        /// </summary>
        public string getPoolName(string sitename)
        {
            string poolname = "";
            try
            {
                using (ServerManager iisManager = new ServerManager())
                {
                    Configuration cfg = iisManager.GetApplicationHostConfiguration();
                    ConfigurationSection sitesection = cfg.GetSection("system.applicationHost/sites");
                    var sitecollection = sitesection.GetCollection();
                    foreach (var siteinfo in sitecollection)
                    {
                        if (siteinfo.GetAttributeValue("name").ToString() == sitename)
                        {
                            poolname = siteinfo.GetCollection()[0].GetAttributeValue("applicationPool").ToString();
                        }
                    }
                    iisManager.CommitChanges();
                }
            }
            catch { }
            return poolname;
        }
        /// <summary>
        /// 判断Windows用户是否存在
        /// </summary>
        /// <param name="strUserName"></param>
        /// <returns></returns>
        public int UserExist(string groupName, string UserName)
        {
            int errorCode = ErrorCode.UserNotExit;

            try
            {
                using (DirectoryEntry AD = new DirectoryEntry(@"WinNT://" + Environment.MachineName + ",computer"))
                {
                    var user = AD.Children.Find(UserName, "user");

                    if (ADUser.GroupSiteExists(groupName, UserName))
                    {
                        errorCode = ErrorCode.UserExist;
                    }

                }
            }
            catch
            {
                errorCode = ErrorCode.UserNotExit;
            }

            return errorCode;
        }
        /// <summary>
        /// 判断目录是否存在
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public int PathExist(string path)
        {
            int errorCode = ErrorCode.PathNotExist;

            if (Directory.Exists(path))
            {
                errorCode = ErrorCode.PathExist;
            }

            return errorCode;
        }
        /// <summary>
        /// iis6 检测站点是否存在
        /// </summary>
        /// <param name="siteName"></param>
        /// <returns></returns>
        public bool iSiteExist(string siteName)
        {
            var siteEntry = new DirectoryEntry("IIS://localhost/w3svc");//IIS对象

            //检查是否已存在，顺便确定端口占用及站点ID占用情况
            bool exists = false;
            foreach (DirectoryEntry site in siteEntry.Children)
            {
                if (!site.SchemaClassName.EqualsEx("IIsWebServer"))		//IIsWebServer代表站点
                    continue;
                string name = Convert.ToString(site.Properties["ServerComment"].Value);
                if (!name.IsNullOrEmpty() && name.EqualsEx(siteName))
                    exists = true;
            }
            return exists;
        }

        /// <summary>
        /// 站点是否存在
        /// </summary>
        /// <param name="siteName"></param>
        /// <returns></returns>
        public int SiteExist(string siteName)
        {
            int errorCode = ErrorCode.SiteNotFound;

            try
            {
                ServerManager server = new ServerManager();//创建iis管理对象
                //找指定站点
                foreach (var site in server.Sites)
                {
                    if (site.Name.EqualsEx(siteName))
                    {
                        errorCode = ErrorCode.SiteExists;
                        break;
                    }
                }
            }
            catch
            {
                errorCode = ErrorCode.FindSiteError;
            }

            return errorCode;
        }
        /// <summary>
        /// 设置日志文件开启状态
        /// </summary>
        /// <param name="siteName"></param>
        /// <param name="Connections"></param>
        public static bool SetLog(string siteName, long logstatus)
        {
            using (ServerManager iisManager = new ServerManager())
            {
                try
                {
                    Site site = iisManager.Sites[siteName];
                    if (site == null) { return false; }
                    SiteLogFile logfile = site.LogFile;
                    if (logstatus == 1) { logfile.Enabled = true; } else { logfile.Enabled = false; }
                    iisManager.CommitChanges();
                }
                catch
                {
                    return false;
                }
            }
            return true;
        }
        /// <summary>
        /// 启动站点
        /// </summary>
        /// <param name="sitename"></param>
        public int siteStart(string sitename)
        {
            ServerManager iisManger = new ServerManager();
            int errorCode = ErrorCode.siteStart;

            try
            {
                //启动IIS
                if (iisStart() == ErrorCode.iisStateError)
                {
                    errorCode = ErrorCode.iisStateError;
                    return errorCode;
                }
                //找指定站点
                foreach (var site in iisManger.Sites)
                {
                    if (site.Name.EqualsEx(sitename))
                    {
                         site.Start();
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                NLogger.Error(ex.ToString());
                errorCode = ErrorCode.siteStateError;
            }
            iisManger.CommitChanges();

            return errorCode;
        }
        public int siteStord(string sitename)
        {
            ServerManager iisManger = new ServerManager();
            int errorCode = ErrorCode.siteStart;

            try
            {
                //启动IIS
                if (iisStart() == ErrorCode.iisStateError)
                {
                    errorCode = ErrorCode.iisStateError;
                    return errorCode;
                }
                //找指定站点
                foreach (var site in iisManger.Sites)
                {
                    if (site.Name.EqualsEx(sitename))
                    {
                        site.Stop();
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                NLogger.Error(ex.ToString());
                errorCode = ErrorCode.siteStateError;
            }
            iisManger.CommitChanges();

            return errorCode;
        }


        /// <summary>
        /// iis6启动站点
        /// </summary>
        /// <param name="sitename"></param>
        public int isiteStart(string sitename)
        {
            int errorCode = ErrorCode.siteStart;

            var siteEntry = new DirectoryEntry("IIS://localhost/w3svc");//IIS6管理对象
            try
            {
                foreach (DirectoryEntry child in siteEntry.Children)
                {
                    if (child.SchemaClassName == "IIsWebServer" && child.Properties["ServerComment"].Value.ToString() == sitename)
                    {
                        child.Invoke("Start", new object[] { });//启动站点
                    }
                }
            }
            catch (Exception ex)
            {
                NLogger.Error(ex.ToString());
                errorCode = ErrorCode.siteStateError;
            }

            return errorCode;
        }

        public int isiteStord(string sitename)
        {
            int errorCode = ErrorCode.siteStart;

            var siteEntry = new DirectoryEntry("IIS://localhost/w3svc");//IIS6管理对象
            try
            {
                foreach (DirectoryEntry child in siteEntry.Children)
                {
                    if (child.SchemaClassName == "IIsWebServer" && child.Properties["ServerComment"].Value.ToString() == sitename)
                    {
                        child.Invoke("Stop", new object[] { });//启动站点
                    }
                }
            }
            catch (Exception ex)
            {
                NLogger.Error(ex.ToString());
                errorCode = ErrorCode.siteStateError;
            }

            return errorCode;
        }


        /// <summary>
        /// 设置站点文件夹权限
        /// </summary>
        public void icaclsSet(string username, string sitepath)
        {
            // if root path
            if (Extensions.EqualsRootPath(sitepath))
            {
                return;
            }
            try
            {
                string bat = sitepath + " /c" + " /grant:r " + username + ":" + "(OI)(CI)(R,D,W,WDAC,WO,DC)" + " /t";
                System.Diagnostics.ProcessStartInfo myStartInfo = new System.Diagnostics.ProcessStartInfo("icacls", bat);//启动iis
                System.Diagnostics.Process myProcess = new System.Diagnostics.Process();

                myProcess.StartInfo = myStartInfo;
                myProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                //添加用户权限
                myProcess.Start();
                myProcess.WaitForExit();//等待程序退出
            }
            catch (Exception ex)
            {
                NLogger.Error(ex.ToString());
            }
        }

        /// <summary>
        /// 设置站点文件夹权限
        /// </summary>
        public void icaclsSet(bool rw,  string username, string sitepath)
        {
            // if root path
            if (Extensions.EqualsRootPath(sitepath))
            {
                return;
            }
            try
            {
                string bat = sitepath + " /c" + " /grant:r " + username + ":" + "(OI)(CI)(R,D,W,WDAC,WO,DC)" + " /t";

                if(rw==false)
                     bat = sitepath + " /c" + " /grant:r " + username + ":" + "(OI)(CI)(R,D,WDAC,WO,DC)" + " /t";

                NLogger.Info(bat);
                System.Diagnostics.ProcessStartInfo myStartInfo = new System.Diagnostics.ProcessStartInfo("icacls", bat);//启动iis
                System.Diagnostics.Process myProcess = new System.Diagnostics.Process();

                myProcess.StartInfo = myStartInfo;
                myProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                //添加用户权限
                myProcess.Start();
                myProcess.WaitForExit();//等待程序退出
            }
            catch (Exception ex)
            {
                NLogger.Error(ex.ToString());
            }
        }




        /// <summary>
        /// 移除站点文件夹权限
        /// </summary>
        public void icaclsRemove(string username, string sitepath)
        {
            // if root path
            if (Extensions.EqualsRootPath(sitepath))
            {
                return;
            }
            try
            {
                string bat = sitepath + " /remove " +  username ;
                System.Diagnostics.ProcessStartInfo myStartInfo = new System.Diagnostics.ProcessStartInfo("icacls", bat);//启动iis
                System.Diagnostics.Process myProcess = new System.Diagnostics.Process();

                myProcess.StartInfo = myStartInfo;
                myProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                //添加用户权限
                myProcess.Start();
                myProcess.WaitForExit();//等待程序退出
            }
            catch (Exception ex)
            {
                NLogger.Error(ex.ToString());
            }
        }
        /// <summary>
        /// 移除站点文件夹权限
        /// </summary>
        public void icaclsReset(string sitepath)
        {
            // if root path
            if (Extensions.EqualsRootPath(sitepath))
            {
                return;
            }
            try
            {
                string bat = sitepath + " /reset /T";
                System.Diagnostics.ProcessStartInfo myStartInfo = new System.Diagnostics.ProcessStartInfo("icacls", bat);//启动iis
                System.Diagnostics.Process myProcess = new System.Diagnostics.Process();

                myProcess.StartInfo = myStartInfo;
                myProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                //添加用户权限
                myProcess.Start();
                myProcess.WaitForExit();//等待程序退出
            }
            catch
            {
            }
        }
        public bool FileSP_Exists(string path, string username)
        {
            /*---- try -----*/
            try
            {
                if (!Directory.Exists(path))
                {
                    return false;
                }
                //directory
                DirectoryInfo folder = new DirectoryInfo(path);
                /*---- Directory security ----*/
                DirectorySecurity dSecurity = folder.GetAccessControl(AccessControlSections.All);       
                /*---- rules ----*/
                AuthorizationRuleCollection rules = dSecurity.GetAccessRules(true, true, typeof(System.Security.Principal.SecurityIdentifier));
                //iterator
                foreach (AuthorizationRule rule in rules)
                {
                    try
                    {
                        /*---- account ----*/
                        System.Security.Principal.NTAccount account =
                        (System.Security.Principal.NTAccount)rule.IdentityReference.Translate(typeof(System.Security.Principal.NTAccount));
                        string current_account = account.Value;
                        /*--- get exact account ----*/
                        try
                        {
                            current_account = current_account.Substring(current_account.LastIndexOf(@"\") + 1);
                        }
                        catch
                        {

                        }
                        //jduge
                        if (current_account == username)
                        {
                            return true;
                        }
                    }
                    catch
                    {
                        //doint nothing
                    }
                }
            }
            catch
            {
                //mothing
            }
            return false;
        }
        /// <summary>
        /// 启动IIS
        /// </summary>
        public static int iisStart()
        {
            int errorCode = ErrorCode.iisStart;
            try
            {
                System.Diagnostics.ProcessStartInfo myStartInfo = new System.Diagnostics.ProcessStartInfo("iisreset", "/start");//启动iis
                System.Diagnostics.Process myProcess = new System.Diagnostics.Process();

                myProcess.StartInfo = myStartInfo;
                myProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                //进程启动iis
                myProcess.Start();
                myProcess.WaitForExit();//等待程序退出
            }
            catch
            {
                errorCode = ErrorCode.iisStateError;
            }
            return errorCode;
        }
        /// <summary>
        /// 停止IIS
        /// </summary>
        public static int iisStop()
        {
            int errorCode = ErrorCode.iisStop;
            try
            {
                System.Diagnostics.ProcessStartInfo myStartInfo = new System.Diagnostics.ProcessStartInfo("iisreset", "/stop");//停止iis
                System.Diagnostics.Process myProcess = new System.Diagnostics.Process();

                myProcess.StartInfo = myStartInfo;
                myProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                //进程启动iis
                myProcess.Start();
                myProcess.WaitForExit();//等待程序退出
            }
            catch
            {
                errorCode = ErrorCode.iisStateError;
            }
            return errorCode;
        }
        /// <summary>
        /// 重启IIS
        /// </summary>
        public static bool iisRestart()
        {
            try
            {
                System.Diagnostics.ProcessStartInfo myStartInfo = new System.Diagnostics.ProcessStartInfo("iisreset");//重启iis
                System.Diagnostics.Process myProcess = new System.Diagnostics.Process();

                myProcess.StartInfo = myStartInfo;
                myProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                //进程启动iis
                myProcess.Start();
                myProcess.WaitForExit();//等待程序退出
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 设置默认文档
        /// </summary>
        /// <param name="siteName"></param>
        /// <param name="defaultDocuments"></param>
        /// <param name="enabled"></param>
        public int SetDefaultDocument(string siteName, List<string> defaultDocuments, bool enabled)
        {
            int errorCode = ErrorCode.setDefaultDocOK;
            /*
             * 移除默认站点
             */
            //List<string> tdefaultDocuments = GetDefaultDocument();
            //foreach (string tdefualtDocument in tdefaultDocuments)
            //{
            //    if(defaultDocuments.Contains(tdefualtDocument))
            //    {
            //        defaultDocuments.Remove(tdefualtDocument);
            //    }
            //}
            if (defaultDocuments.Contains("default.aspx"))
            {
                defaultDocuments.Remove("default.aspx");
            }

            using (ServerManager iisManager = new ServerManager())
            {
                try
                {
                    Configuration cfg = iisManager.GetApplicationHostConfiguration();
                    ConfigurationSection defaultDocumentSection = cfg.GetSection("system.webServer/defaultDocument", siteName);

                    if (!enabled)
                    {
                        defaultDocumentSection.SetAttributeValue("enabled", enabled);
                        iisManager.CommitChanges();
                        return errorCode;
                    }

                    ConfigurationElement filesElement = defaultDocumentSection.GetChildElement("files");
                    ConfigurationElementCollection filesCollection = filesElement.GetCollection();
                    filesCollection.Clear();
                    foreach (string defaultDocument in defaultDocuments)
                    {
                        try
                        {
                            ConfigurationElement defaultDocElement = filesCollection.CreateElement();
                            defaultDocElement.SetAttributeValue("value", defaultDocument);
                            filesCollection.Add(defaultDocElement);
                        }
                        catch
                        {

                        }
                    }
                    defaultDocumentSection.SetAttributeValue("enabled", true);
                    iisManager.CommitChanges();
                }
                catch
                {
                    iisManager.Dispose();
                    errorCode = ErrorCode.setDefaultDocFail;
                }

                return errorCode;
            }

        }
        /// <summary>
        /// 获取首页信息
        /// </summary>
        /// <param name="siteName"></param>
        /// <param name="defaultDocuments"></param>
        /// <param name="enabled"></param>
        /// <returns></returns>
        public string GetDefaultDocument(string siteName)
        {
            string homepages = "";
            using (ServerManager iisManager = new ServerManager())
            {
                try
                {
                    Configuration cfg = iisManager.GetApplicationHostConfiguration();
                    ConfigurationSection defaultDocumentSection = cfg.GetSection("system.webServer/defaultDocument", siteName);

                    ConfigurationElement filesElement = defaultDocumentSection.GetChildElement("files");
                    ConfigurationElementCollection filesCollection = filesElement.GetCollection();

                    foreach (var defaultDocument in filesCollection)
                    {
                        homepages += defaultDocument["value"].ToString() + ",";
                    }
                    homepages = homepages.Substring(0, homepages.Length - 1);
                }
                catch
                {
                }
               
            }
            return homepages;

        }
        private void CloseGdefaultDocument()
        {
            using (ServerManager iisManager = new ServerManager())
            {
                try
                {
                    Configuration cfg = iisManager.GetApplicationHostConfiguration();
                    /*
                    * 设置全局默认文档为false
                    */
                    ConfigurationSection gdefaultDocumentSection = cfg.GetSection("system.webServer/defaultDocument");
                    gdefaultDocumentSection.SetAttributeValue("enabled", false);
                    iisManager.CommitChanges();
                }
                catch
                {
                    iisManager.Dispose();
                }
            }
        }
        /// <summary>
        /// 获取指定站点的默认文档
        /// </summary>
        /// <param name="siteName"></param>
        /// <returns></returns>
        public static string GetSiteDefaultDocument(string siteName)
        {
            String sb = "";
            using (ServerManager iisManager = new ServerManager())
            {
                Configuration cfg = iisManager.GetApplicationHostConfiguration();
                ConfigurationSection defaultDocumentSection = cfg.GetSection("system.webServer/defaultDocument", siteName);

                ConfigurationElementCollection filesElement = defaultDocumentSection.GetCollection("files");
                foreach (ConfigurationElement defaultDocument in filesElement)
                {
                    if (sb != "") { sb += ","; }
                    //sb += defaultDocument["value"];
                }
            }
            return sb.ToString();
        }
        /// <summary>
        /// 获取默认文档
        /// </summary>
        /// <param name="siteName"></param>
        /// <returns></returns>
        public List<string> GetDefaultDocument()
        {
            List<string> defaultDocuments = new List<string>();
            using (ServerManager iisManager = new ServerManager())
            {
                Configuration cfg = iisManager.GetApplicationHostConfiguration();
                ConfigurationSection defaultDocumentSection = cfg.GetSection("system.webServer/defaultDocument");

                ConfigurationElementCollection filesElement = defaultDocumentSection.GetCollection("files");
                foreach (ConfigurationElement defaultDocument in filesElement)
                {
                    defaultDocuments.Add(defaultDocument.GetAttributeValue("value").ToString());
                }
            }
            return defaultDocuments;
        }
        /// <summary>
        /// 启用asp父目录
        /// </summary>
        public void SetAspParent()
        {
            /*
            * 判断全局变量asp启用父目录是否打开
            */
            using (ServerManager server = new ServerManager())
            {
                try
                {
                    Configuration config = server.GetApplicationHostConfiguration();
                    ConfigurationSection aspsection = config.GetSection("system.webServer/asp");
                    string aspstate = aspsection.GetAttributeValue("enableParentPaths").ToString();
                    if (aspstate == "False")
                    {
                        aspsection.SetAttributeValue("enableParentPaths", true);
                        aspsection.SetAttributeValue("scriptErrorSentToBrowser", true);
                    }
                    server.CommitChanges();
                }
                catch { }
            }
        }
        /// <summary> 
        /// 随机生成FTP密码
        /// </summary>
        public static string MakeFTPPassword(int pwdLength)
        {
            //声明要返回的字符串    
            string password = "";
            //密码中包含的字符数组    
            string pwdchars = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            //数组索引随机数    
            int iRandNum;
            //随机数生成器    
            Random rnd = new Random();
            for (int i = 0; i < pwdLength; i++)
            {
                //Random类的Next方法生成一个指定范围的随机数     
                iRandNum = rnd.Next(pwdchars.Length);
                //tmpstr随机添加一个字符     
                password += pwdchars[iRandNum];
            }
            return password;
        }
        /// <summary>
        /// 设置站点CPU限制和内存限制
        /// </summary>
        /// <param name="sitename"></param>
        public void SiteLimited(string sitename, string cpulimit, string memeory)
        {
            using (ServerManager iisManager = new ServerManager())
            {
                try
                {
                    if (cpulimit != "0")
                    {
                        var cpu = iisManager.ApplicationPools[sitename].Cpu;
                        cpu.Action = ProcessorAction.KillW3wp;
                        cpu.Limit = Convert.ToInt32(cpulimit) * 1000;
                        cpu.ResetInterval = new TimeSpan(0, 1, 0);            
                    }
                    if (memeory != "0")
                    {
                        ApplicationPoolRecycling recycling = iisManager.ApplicationPools[sitename].Recycling;
                        recycling.PeriodicRestart.Memory = Convert.ToInt32(memeory) * 1024;
                        recycling.PeriodicRestart.PrivateMemory = Convert.ToInt32(memeory) * 1024;
                        recycling.PeriodicRestart.Time = new TimeSpan(6, 0, 0);
                    }
                    iisManager.CommitChanges();
                }
                catch { }

            }
        }
        /// <summary>
        /// 设置全局PHP引擎
        /// </summary>
        /// <param name="phppath"></param>
        public void SetGPhpEngine(string phppath)
        {
            try
            {
                using (ServerManager serverManager = new ServerManager())
                {
                    Configuration config = serverManager.GetApplicationHostConfiguration();
                    ConfigurationSection handlersSection = config.GetSection("system.webServer/handlers");
                    var handlersCollection = handlersSection.GetCollection();
                    if (handlersCollection.Count(o => o.Attributes["name"].Value.ToString() == "PHP-GFastCGI") == 0)
                    {
                        var phpElement = handlersCollection.CreateElement("add");
                        phpElement["name"] = @"PHP-GFastCGI";
                        phpElement["path"] = @"*.php";
                        phpElement["verb"] = "GET,HEAD,POST";
                        phpElement["modules"] = "FastCgiModule";
                        phpElement["scriptProcessor"] = @phppath;
                        phpElement["resourceType"] = "Either";
                        phpElement["requireAccess"] = @"Script";
                        handlersCollection.AddAt(0,phpElement);
                    }
                    serverManager.CommitChanges();
                }
            }
            catch { }
        }
        /// <summary>
        /// 获取PHP引擎信息
        /// </summary>
        /// <param name="sitename">站点名</param>
        /// <returns></returns>
        public string GetPhpEngine(string sitename)
        {
            string phppath = "";
            try
            {
                using (ServerManager serverManager = new ServerManager())
                {
                    Configuration config = serverManager.GetApplicationHostConfiguration();
                    ConfigurationSection handlersSection = config.GetSection("system.webServer/handlers",sitename);
                    var handlersCollection = handlersSection.GetCollection();
                    foreach(ConfigurationElement handler in handlersCollection){
                        if (handler.ElementTagName.ToString() == "add" && handler["name"].ToString() == "PHP-FastCGI")
                        {
                            phppath = handler["scriptProcessor"].ToString();
                            break;
                        }
                        //if (phppath == "" && handler.ElementTagName.ToString() == "add" && handler["name"].ToString() == "PHP-GFastCGI")
                        //{
                        //    phppath = handler["scriptProcessor"].ToString();
                        //    break;
                        //}
                        else
                        {
                            phppath = "";
                        }
                    }
                    //if (handlersCollection.Count(o => o.Attributes["name"].Value.ToString() == "PHP-GFastCGI") == 0)
                    //{
                    //    var phpcgi = handlersCollection.FirstOrDefault(o => o.Attributes["name"].Value.ToString() == "PHP-GFastCGI");
                    //    phppath = phpcgi["scriptProcessor"].ToString();
                    //}
                }
            }
            catch 
            {
                
            }
            return phppath;
        }
        /// <summary>
        /// 设置当前选中站点的php引擎
        /// </summary>
        public void NSetPhpEngine(string sitename, string phppath)
        {
            SSetPhpEngine(sitename, phppath);//添加FastCgi支持
            SetGPhpEngine(phppath);//设置全局PHP引擎
            try
            {
                using (ServerManager serverManager = new ServerManager())
                {
                    Configuration config = serverManager.GetApplicationHostConfiguration();
                    /*
                    * 添加站点php引擎
                    */
                    ConfigurationSection handlersSection = config.GetSection("system.webServer/handlers", sitename);
                    var handlersCollection = handlersSection.GetCollection();

                    if (handlersCollection.Count(o => o.Attributes["name"].Value.ToString() == "PHP-GFastCGI") == 0)
                    {
                        if (handlersCollection.Count(o => o.Attributes["name"].Value.ToString() == "PHP-FastCGI") == 0)
                        {
                            var phpElement = handlersCollection.CreateElement("add");
                            phpElement["name"] = @"PHP-FastCGI";
                            phpElement["path"] = @"*.php";
                            phpElement["verb"] = "GET,HEAD,POST";
                            phpElement["modules"] = "FastCgiModule";
                            phpElement["scriptProcessor"] = @phppath;
                            phpElement["resourceType"] = "Either";
                            phpElement["requireAccess"] = @"Script";
                            handlersCollection.AddAt(0, phpElement);
                        }
                        else
                        {
                            var phpElement = handlersCollection.FirstOrDefault(o => o.Attributes["name"].Value.ToString() == "PHP-FastCGI");
                            phpElement["name"] = @"PHP-FastCGI";
                            phpElement["path"] = @"*.php";
                            phpElement["verb"] = "GET,HEAD,POST";
                            phpElement["modules"] = "FastCgiModule";
                            phpElement["scriptProcessor"] = @phppath;
                            phpElement["resourceType"] = "Either";
                            phpElement["requireAccess"] = @"Script";
                        }
                    }
                    else
                    {
                        var phpElement = handlersCollection.FirstOrDefault(o => o.Attributes["name"].Value.ToString() == "PHP-GFastCGI");
                        phpElement["name"] = @"PHP-FastCGI";
                        phpElement["path"] = @"*.php";
                        phpElement["verb"] = "GET,HEAD,POST";
                        phpElement["modules"] = "FastCgiModule";
                        phpElement["scriptProcessor"] = @phppath;
                        phpElement["resourceType"] = "Either";
                        phpElement["requireAccess"] = @"Script";
                    }
                    //提交更改
                    serverManager.CommitChanges();
                }
            }
            catch
            {
            }
        }
        /// <summary>
        /// 删除站点的php引擎
        /// </summary>
        public void WSetPhpEngine(string sitename)
        {
            try
            {
                using (ServerManager serverManager = new ServerManager())
                {
                    Configuration config = serverManager.GetApplicationHostConfiguration();
                    /*
                    * 添加站点php引擎
                    */
                    ConfigurationSection handlersSection = config.GetSection("system.webServer/handlers", sitename);
                    var handlersCollection = handlersSection.GetCollection();
                    //handlersCollection.Clear();
                    if (handlersCollection.Count(o => o.Attributes["name"].Value.ToString() == "PHP-FastCGI") != 0)
                    {
                        var phpElement = handlersCollection.FirstOrDefault(o => o.Attributes["name"].Value.ToString() == "PHP-FastCGI");
                        handlersCollection.Remove(phpElement);
                    }
                    //提交更改
                    serverManager.CommitChanges();
                }
            }
            catch
            {
            }
        }
        /// <summary>
        /// 设置PHP引擎 这个方法有点问题 只针对云计算机
        /// </summary>
        /// <param name="sitename"></param>
        /// <param name="phppath"></param>
        public void oSetPhpEngine(string sitename, string phppath)
        {
            if (System.IO.File.Exists(phppath))//判断php文件是否存在
            {
                try
                {
                    /*
                    * 判断php引擎是否存在
                    */
                    using (ServerManager serverManager = new ServerManager())
                    {
                        Configuration config = serverManager.GetApplicationHostConfiguration();
                        ConfigurationSection phpcgiSection = config.GetSection("system.webServer/fastCgi");
                        ConfigurationElementCollection phpcgiCollection = phpcgiSection.GetCollection();
                        int count = phpcgiCollection.Count();
                        if (count == 0)
                        {
                            /*
                            *  不存在添加
                            */
                            var phpelement = phpcgiCollection.CreateElement();
                            phpelement["fullPath"] = phppath;
                            phpelement["instanceMaxRequests"] = 10000;
                            phpcgiCollection.Add(phpelement);
                        }
                        else
                        {
                             bool flag = false;
                            /*
                            * 更改php引擎
                            */
                            foreach (ConfigurationElement phpcgi in phpcgiCollection)
                            {
                                if (phpcgi.GetAttributeValue("fullPath").ToString().ToLower() == phppath.ToLower())
                                {
                                    flag = true;
                                }
                            }
                            /*
                            * 添加
                            */
                            if (!flag)
                            {
                                var phpelement = phpcgiCollection.CreateElement();
                                phpelement["fullPath"] = phppath;
                                phpelement["instanceMaxRequests"] = 10000;
                                phpcgiCollection.Add(phpelement);
                            }
                        }
                        /*
                         * 添加站点php引擎
                         */
                        ConfigurationSection handlersSection = config.GetSection("system.webServer/handlers", sitename);
                        var handlersCollection = handlersSection.GetCollection();

                        if (handlersCollection.Count(o => o.Attributes["name"].Value.ToString() == "PHP-GFastCGI") == 0)
                        {
                            return;
                        }
                        else
                        {
                            var phpElement = handlersCollection.FirstOrDefault(o => o.Attributes["name"].Value.ToString() == "PHP-GFastCGI");
                            phpElement["name"] = @"PHP-FastCGI";
                            phpElement["path"] = @"*.php";
                            phpElement["verb"] = "GET,HEAD,POST";
                            phpElement["modules"] = "FastCgiModule";
                            phpElement["scriptProcessor"] = @phppath;
                            phpElement["resourceType"] = "Either";
                            phpElement["requireAccess"] = @"Script";
                        }
                        //提交更改
                        serverManager.CommitChanges();
                    }
                }
                catch { }
            }
        }
        /// <summary>
        /// 设置PHP引擎
        /// </summary>
        /// <param name="sitename"></param>
        /// <param name="phppath"></param>
        public void SetPhpEngine(string sitename, string phppath)
        {
            //if (System.IO.File.Exists(phppath))//判断php文件是否存在
            //{ 不做判断了 即时不存在该文件也给添加引擎
            try
            {
                /*
                * 判断php引擎是否存在
                */
                using (ServerManager serverManager = new ServerManager())
                {
                    Configuration config = serverManager.GetApplicationHostConfiguration();
                    ConfigurationSection phpcgiSection = config.GetSection("system.webServer/fastCgi");
                    ConfigurationElementCollection phpcgiCollection = phpcgiSection.GetCollection();
                    int count = phpcgiCollection.Count();
                    if (count == 0)
                    {
                        /*
                        *  不存在添加
                        */
                        var phpelement = phpcgiCollection.CreateElement();
                        phpelement["fullPath"] = phppath;
                        phpelement["instanceMaxRequests"] = 10000;
                        phpcgiCollection.Add(phpelement);
                    }
                    else
                    {
                        bool flag = false;
                        /*
                        * 更改php引擎
                        */
                        foreach (ConfigurationElement phpcgi in phpcgiCollection)
                        {
                            if (phpcgi.GetAttributeValue("fullPath").ToString().ToLower() == phppath.ToLower())
                            {
                                flag = true;
                            }
                        }
                        if (!flag)
                        {
                            /*
                            *  不存在添加
                            */
                            var phpelement = phpcgiCollection.CreateElement();
                            phpelement["fullPath"] = phppath;
                            phpelement["instanceMaxRequests"] = 10000;
                            phpcgiCollection.Add(phpelement);
                        }
                    }
                    /*
                    * 添加站点php引擎
                    */
                    ConfigurationSection handlersSection = config.GetSection("system.webServer/handlers", sitename);
                    var handlersCollection = handlersSection.GetCollection();

                    if (handlersCollection.Count(o => o.Attributes["name"].Value.ToString() == "PHP-GFastCGI") == 0)
                    {
                        return;
                    }
                    else
                    {
                        var phpElement = handlersCollection.FirstOrDefault(o => o.Attributes["name"].Value.ToString() == "PHP-GFastCGI");
                        phpElement["name"] = @"PHP-FastCGI";
                        phpElement["path"] = @"*.php";
                        phpElement["verb"] = "GET,HEAD,POST";
                        phpElement["modules"] = "FastCgiModule";
                        phpElement["scriptProcessor"] = @phppath;
                        phpElement["resourceType"] = "Either";
                        phpElement["requireAccess"] = @"Script";
                    }
                    //提交更改
                    serverManager.CommitChanges();
                }
            }
            catch { }
            //}
        }
        /// <summary>
        /// 设置PHP引擎
        /// </summary>
        /// <param name="sitename"></param>
        /// <param name="phppath"></param>
        public void SSetPhpEngine(string sitename, string phppath)
        {
            if (System.IO.File.Exists(phppath))//判断php文件是否存在
            {
                try
                {
                    /*
                    * 判断php引擎是否存在
                    */
                    using (ServerManager serverManager = new ServerManager())
                    {
                        Configuration config = serverManager.GetApplicationHostConfiguration();
                        ConfigurationSection phpcgiSection = config.GetSection("system.webServer/fastCgi");
                        ConfigurationElementCollection phpcgiCollection = phpcgiSection.GetCollection();
                        int count = phpcgiCollection.Count();
                        if (count == 0)
                        {
                            /*
                            *  不存在添加
                            */
                            var phpelement = phpcgiCollection.CreateElement();
                            phpelement["fullPath"] = phppath;
                            phpelement["instanceMaxRequests"] = 10000;
                            phpcgiCollection.Add(phpelement);
                        }
                        else
                        {
                            bool flag = false;
                            /*
                            * 更改php引擎
                            */
                            foreach (ConfigurationElement phpcgi in phpcgiCollection)
                            {
                                if (phpcgi.GetAttributeValue("fullPath").ToString().ToLower() == phppath.ToLower())
                                {
                                    flag = true;
                                }
                            }
                            if (!flag)
                            {
                                /*
                                *  不存在添加
                                */
                                var phpelement = phpcgiCollection.CreateElement();
                                phpelement["fullPath"] = phppath;
                                phpelement["instanceMaxRequests"] = 10000;
                                phpcgiCollection.Add(phpelement);
                            }
                        }
                        //提交更改
                        serverManager.CommitChanges();
                    }
                }
                catch { }
            }
        }
        #region 获取程序版本设置字符串
        /// <summary>
        /// 获取程序版本设置字符串
        /// </summary>
        /// <returns></returns>
        public string[] _GetAspNetVersion(string phppath, string netversion, int siteid)
        {

            string ScriptMaps = "";//Maps
            string WindowsPath = Environment.GetEnvironmentVariable("windir");
            string FramworkPath = WindowsPath + @"\microsoft.net\framework\v" + System.Environment.Version.ToString(3);
            if (phppath != "")
            {
                string fastCgiPath = @"C:\WINDOWS\system32\inetsrv\fcgiext.dll";
                string fastiniPath = @"C:\WINDOWS\system32\inetsrv\fcgiext.ini";

                var siteEntry = new System.DirectoryServices.DirectoryEntry("IIS://localhost/w3svc");//IIS6管理对象
                object[] scriptMaps = (object[])siteEntry.Properties["ScriptMaps"].Value;
                bool flag = false;
                foreach (object scriptMap in scriptMaps)//判断全局变量中是否已经配置PHP引擎
                {
                    if (scriptMap.ToString().Substring(0, 4).ToLower() == ".php" && scriptMap.ToString().Contains("fcgiext.dll"))
                    {
                        flag = true;
                    }
                }
                if (!flag)//没有配置 那就配置
                {
                    string fastCgi = ".php," + fastCgiPath + @",5,GET,HEAD,POST,TRACE";
                    siteEntry.Properties["ScriptMaps"].Add(fastCgi);
                    siteEntry.CommitChanges();
                }
                if (System.IO.File.Exists(fastiniPath))//更改fastCGI配置文件设置PHP路径
                {
                    //先判断全局PHP路径是否已经配置
                    string gphpcgi = INIOperationClass.INIGetStringValue(fastiniPath, "PHP", "ExePath", "");
                    if (gphpcgi == "")
                    {
                        //没配置就配置
                        INIOperationClass.INIWriteValue(fastiniPath, "Types", "PHP", "PHP");
                        INIOperationClass.INIWriteValue(fastiniPath, "PHP", "ExePath", phppath);
                    }

                    int i = 0;
                    while (true)
                    {
                        string phpcgi = INIOperationClass.INIGetStringValue(fastiniPath, "PHP" + i, "ExePath", "");
                        if (phpcgi != "")
                        {
                            i++;
                            continue;
                        }
                        else
                        {
                            break;
                        }
                    }
                    //将该站点绑定到当前php路径
                    INIOperationClass.INIWriteValue(fastiniPath, "Types", "PHP:" + siteid, "PHP" + i);
                    INIOperationClass.INIWriteValue(fastiniPath, "PHP" + i, "ExePath", phppath);
                    INIOperationClass.INIWriteValue(fastiniPath, "Types", "PHP:" + siteid, "PHP" + i);
                }

                string SystemPath = System.Environment.SystemDirectory;
                if (netversion == "v4.0")
                {
                    FramworkPath = WindowsPath + @"\microsoft.net\framework\v" + System.Environment.Version.ToString(3);
                }
                if (netversion == "v2.0" || netversion == "")
                {
                    if (System.IO.Directory.Exists(WindowsPath + @"\microsoft.net\framework" + @"\v2.0.50727"))
                    {
                        FramworkPath = WindowsPath + @"\microsoft.net\framework" + @"\v2.0.50727";
                    }
                }

                ScriptMaps = @".asp," + SystemPath + @"\inetsrv\asp.dll,5,GET,HEAD,POST,TRACE
        .php," + fastCgiPath + @",5,GET,HEAD,POST,TRACE
        .cer," + SystemPath + @"\inetsrv\asp.dll,5,GET,HEAD,POST,TRACE  
        .cdx," + SystemPath + @"\inetsrv\asp.dll,5,GET,HEAD,POST,TRACE  
        .asa," + SystemPath + @"\inetsrv\asp.dll,5,GET,HEAD,POST,TRACE  
        .idc," + SystemPath + @"\inetsrv\httpodbc.dll,5,GET,POST  
        .shtm," + SystemPath + @"\inetsrv\ssinc.dll,5,GET,POST  
        .shtml," + SystemPath + @"\inetsrv\ssinc.dll,5,GET,POST  
        .stm," + SystemPath + @"\inetsrv\ssinc.dll,5,GET,POST  
        .asax," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .ascx," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .ashx," + FramworkPath + @"\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG  
        .asmx," + FramworkPath + @"\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG  
        .aspx," + FramworkPath + @"\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG  
        .axd," + FramworkPath + @"\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG  
        .vsdisco," + FramworkPath + @"\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG  
        .rem," + FramworkPath + @"\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG  
        .soap," + FramworkPath + @"\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG  
        .config," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .cs," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .csproj," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .vb," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .vbproj," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .webinfo," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .licx," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .resx," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .resources," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .xoml," + FramworkPath + @"\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG  
        .rules," + FramworkPath + @"\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG  
        .master," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .skin," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .compiled," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .browser," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .mdb," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .jsl," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .vjsproj," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .sitemap," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .msgx," + FramworkPath + @"\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG  
        .ad," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .dd," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .ldd," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .sd," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .cd," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .adprototype," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .lddprototype," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .sdm," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .sdmDocument," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .ldb," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .svc," + FramworkPath + @"\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG  
        .mdf," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .ldf," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .java," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .exclude," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .refresh," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
        .soap," + FramworkPath + @"\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG
	    .rem," + FramworkPath + @"\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG
	    .vsdisco," + FramworkPath + @"\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG
	    .webinfo," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
	    .aspx," + FramworkPath + @"\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG";
            }
            else
            {
                string SystemPath = System.Environment.SystemDirectory;
                if (netversion == "v4.0")
                {
                    FramworkPath = WindowsPath + @"\microsoft.net\framework\v" + System.Environment.Version.ToString(3);
                }
                if (netversion == "v2.0" || netversion == "")
                {
                    if (System.IO.Directory.Exists(WindowsPath + @"\microsoft.net\framework" + @"\v2.0.50727"))
                    {
                        FramworkPath = WindowsPath + @"\microsoft.net\framework" + @"\v2.0.50727";
                    }
                }

                ScriptMaps = @".asp," + SystemPath + @"\inetsrv\asp.dll,5,GET,HEAD,POST,TRACE
        .cer," + SystemPath + @"\inetsrv\asp.dll,5,GET,HEAD,POST,TRACE  
        .cdx," + SystemPath + @"\inetsrv\asp.dll,5,GET,HEAD,POST,TRACE  
        .asa," + SystemPath + @"\inetsrv\asp.dll,5,GET,HEAD,POST,TRACE  
        .idc," + SystemPath + @"\inetsrv\httpodbc.dll,5,GET,POST  
        .shtm," + SystemPath + @"\inetsrv\ssinc.dll,5,GET,POST  
        .shtml," + SystemPath + @"\inetsrv\ssinc.dll,5,GET,POST  
        .stm," + SystemPath + @"\inetsrv\ssinc.dll,5,GET,POST  
        .asax," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .ascx," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .ashx," + FramworkPath + @"\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG  
        .asmx," + FramworkPath + @"\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG  
        .aspx," + FramworkPath + @"\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG  
        .axd," + FramworkPath + @"\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG  
        .vsdisco," + FramworkPath + @"\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG  
        .rem," + FramworkPath + @"\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG  
        .soap," + FramworkPath + @"\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG  
        .config," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .cs," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .csproj," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .vb," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .vbproj," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .webinfo," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .licx," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .resx," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .resources," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .xoml," + FramworkPath + @"\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG  
        .rules," + FramworkPath + @"\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG  
        .master," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .skin," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .compiled," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .browser," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .mdb," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .jsl," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .vjsproj," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .sitemap," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .msgx," + FramworkPath + @"\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG  
        .ad," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .dd," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .ldd," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .sd," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .cd," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .adprototype," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .lddprototype," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .sdm," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .sdmDocument," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .ldb," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .svc," + FramworkPath + @"\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG  
        .mdf," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .ldf," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .java," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .exclude," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG  
        .refresh," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
        .soap," + FramworkPath + @"\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG
	    .rem," + FramworkPath + @"\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG
	    .vsdisco," + FramworkPath + @"\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG
	    .webinfo," + FramworkPath + @"\aspnet_isapi.dll,5,GET,HEAD,POST,DEBUG
	    .aspx," + FramworkPath + @"\aspnet_isapi.dll,1,GET,HEAD,POST,DEBUG";
            }

            string[] ret = System.Text.RegularExpressions.Regex.Split(ScriptMaps, "\r\n");
            for (int i = 0; i < ret.Length; i++)
            {
                ret[i] = ret[i].Trim();
            }
            return ret;
        }
        #endregion
    }
}
